#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/10/13 16:19
# @Author  : AshJo
# @Desc    : 回调函数的例子
# @Filename: callback_exp.py
# @Version : v1.0
"""

from concurrent.futures import ThreadPoolExecutor
import requests
import os


# 访问网页，获取网页源代码
def get_page(url):
    print('<进程%s> get %s' % (os.getpid(), url))
    respone = requests.get(url)
    if respone.status_code == 200:
        return {'url': url, 'text': respone.text}


# 获取字典结果后，计算网页源代码的长度，写到文件里。线程执行完毕后绑定回调函数
def parse_page(res):
    res = res.result()
    print('<进程%s> parse %s' % (os.getpid(), res['url']))
    parse_res = 'url:<%s> size:[%s]\n' % (res['url'], len(res['text']))
    with open('db.txt', 'a') as f:
        f.write(parse_res)


if __name__ == '__main__':
    urls = [
        'https://www.baidu.com',
        'https://www.python.org',
        'https://www.linux.org',
        'https://help.github.com/',
        'http://www.sina.com.cn/'
    ]
    # 获取一个线程池对象 = 开启线程池
    tp = ThreadPoolExecutor(4)
    # 循环url列表
    for url in urls:
        # 得到一个future对象 = 把每一个url提交一个get_page任务
        ret = tp.submit(get_page, url)
        # 给future对象绑定一个parse_page回调函数
        ret.add_done_callback(parse_page)
